var service_user = require("../../service/user.js");
var service_parent = require("../../service/parent.js");
var service_son = require("../../service/son.js");
var service_daili = require("../../service/daili.js");
var service_money = require("../../service/money.js");
var service_common = require("../../service/common.js");
var service_version = require("../../service/version.js");
var service_view = require("../../service/view.js");
var service_adv = require("../../service/adv.js");
Page({
	data: {
		userInfo: {},
		select: false,
		daiLi: "",
		money: "",
		parent_text: "",
		isSign: false,
		wrq_news: 0,
		user_news: 0,
		wrq_zan: 0
	},
	onLoad: function() {
		var that = this;
		wx.showLoading({
			title: '加载中',
		})
	},
	refresh: function() {
		wx.showLoading({
			title: '加载中',
		})
		this.onShow();
	},
	toSign: function() {
		wx.navigateTo({
			url: '../sign/sign',
		})
	},
	toTaoBaoKe: function() {
		wx.navigateToMiniProgram({
			appId: 'wx6738ea4b93196331',
		})
	},
	onShow: function() {
		var that = this;
		service_user.login(function(res) {
			wx.hideLoading()

			if (getApp().globalData.userInfo.view_card == 0) {
				that.setData({
					userInfo: getApp().globalData.userInfo,
					select: false,
				})
			} else {
				that.setData({
					userInfo: getApp().globalData.userInfo,
					select: true,
				})
			}
			console.log('userInfo:', getApp().globalData.userInfo);
			if (getApp().globalData.userInfo.isDaiLi == 0.5) {
				that.setData({
					daiLi: "人气大使"
				})
			} else if (getApp().globalData.userInfo.isDaiLi == 1) {
				that.setData({
					daiLi: "初级VIP"
				})
			} else if (getApp().globalData.userInfo.isDaiLi == 2) {
				that.setData({
					daiLi: "中级VIP"
				})
			} else if (getApp().globalData.userInfo.isDaiLi == 3) {
				that.setData({
					daiLi: "高级VIP"
				})
			}
		});
		service_version.getIsAuditing(function(res) {
			if (res.data.sign == 0) {
				getApp().globalData.isSign = 0;
				that.setData({
					isSign: true
				})
			} else {
				getApp().globalData.isSign = 1;
				that.setData({
					isSign: false
				})
			}
		})
		this.User_new()
		// this.wrq_zan()
	},
	goOther: function() {
		wx.navigateTo({
			url: '/pages/other/other?openid=' + getApp().globalData.userInfo.shareopenid,
		})
	},
	Gwang: function() {
		wx.navigateTo({
			url: '/pages/manage/index/index',
		})
	},
	// 获取万人圈点赞个数
	wrq_zan: function() {
		service_common.fetchData(
			service_common.getUrl("user.newLoveCount"), {
				openid: getApp().globalData.openid
			},
			(res) => {
				// 
				this.setData({
					wrq_zan: res.data.data.new_love_count
				})
			},
			"GET"
		)
	},
	// 获取后台消息
	User_new: function() {
		service_adv.getAdvList((res) => {
			console.log('------++++++', res.data.data.data)
			var n = res.data.data.data.length
			service_adv.getSeeAdv(res => {
				this.setData({
					user_news: n - res.data.data.see_adv_list.length
				})
			})
		})
	},
	/**
	 * 获取万人圈新消息数
	 */
	newReplyCount: function(res) {
		this.setData({
			wrq_news: res.data.data.new_reply_count
		})
	},
	//跳转广告列表
	toAdvList: function(e) {
		add_form_id(e.detail.formId);
		console.log(e.detail.formId);
		wx.navigateTo({
			url: "../adv_list/adv_list"
		})
	},
	toTeam: function() {
		wx.switchTab({
			url: '../team_list/team_list',
		})
	},
	// 获取代理余额回调函数
	getMoneyCallBack: function(res) {
		var that = this;
		that.setData({
			money: res.data.data,
		})
		service_parent.getParent(function(res2) {
			console.log('res2:', res2);
			that.setData({
				parent_text: res2.data.data.text,
				parent_openid: res2.data.data.openid,
			})
		});
		service_son.getSonNums(function(res3) {
			var firstNumber = res3.data.data.firstNumber;
			//firstNumber = 200;
			if (firstNumber >= 160 && firstNumber != undefined) {
				service_daili.autoUpgradeDaShi(function(res4) {
					if (res4.data.data.code == 0) {
						wx.hideLoading();
						that.refresh();
					} else {
						wx.hideLoading();
					}
				});
			} else {
				wx.hideLoading();
			}
		})
	},
	toKaiDaiLi: function() {
		wx.navigateTo({
			url: '../kaidaili/kaidaili',
		})
	},
	toCooperation: function() {
		wx.navigateTo({
			url: '../cooperation/cooperation',
		})
	},
	toTeacher: function() {
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.navigateTo({
			url: '../teacher/teacher',
		})
	},
	ziYuanFaBu: function(e) {
		add_form_id(e.detail.formId);
		service_user.getZiYuanFaBuPingTai();
	},
	toCheck: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../check/check',
		})
	},
	toOther: function(e) {
		var that = this;
		// wx.navigateTo({
		//   url: "../other/other?openid=" + that.data.parent_openid,
		// })
		if (getApp().globalData.userInfo.shareopenid == 0 || getApp().globalData.userInfo.shareopenid == "") {
			wx.showToast({
				title: '无推荐人',
				image: "../../image/no_people.png"
			})
		} else {
			wx.navigateTo({
				url: "../other/other?openid=" + getApp().globalData.userInfo.shareopenid,
			})
		}
	},
	toEdit: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../edit/edit',
		})
	},
	daiLiShengQing: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../dailishengqing/dailishengqing',
		})
	},
	toXiaJi: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../xiaji/xiaji',
		})
	},
	jiaoLiuQun: function() {
		wx.showModal({
			title: '提示',
			content: '需要前去下载APP',
			confirmText: "前往",
			success: function(res) {
				if (res.confirm) {
					wx.navigateTo({
						url: '/pages/aApp/aApp',
					})
				}
			}
		})
		// service_user.getQun();
	},
	toJiaoLiu: function() {
		wx.navigateTo({
			url: '../jiao_liu/jiao_liu',
		})
	},
	toCode: function(e) {
		add_form_id(e.detail.formId);
		service_user.getUserCode();
	},
	toHB: function(e) {
		add_form_id(e.detail.formId);
		service_user.getUserHB();
	},
	toKouLing: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../kouling/kouling',
		})
	},
	zhaohezuo: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../zhaohezuo/zhaohezuo',
		})
	},
	showCard: function(e) {
		var that = this;
		if (!e.detail.value) {
			wx.showModal({
				title: '提示',
				content: '您的名片将展示',
				success: function(res) {
					if (res.confirm == true) {
						service_user.showCard(function() {
							getApp().globalData.userInfo["view_card"] = 0;
							wx.hideLoading();
							wx.showToast({
								title: '关闭隐藏',
								duration: 400
							})
						});
					} else {
						that.setData({
							select: !e.detail.value
						})
					}
				}
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '您的名片将隐藏',
				success: function(res) {
					if (res.confirm == true) {
						service_user.hideCard(function() {
							getApp().globalData.userInfo["view_card"] = 1;
							wx.hideLoading();
							wx.showToast({
								title: '打开隐藏',
								duration: 400
							})
						});
					} else {
						that.setData({
							select: !e.detail.value
						})
					}
				}
			})
		}
	},
	toInfo: function(e) {
		add_form_id(e.detail.formId);
		wx.navigateTo({
			url: '../info/info',
		})
	}
})
//添加form_id
function add_form_id(form_id) {
	wx.request({
		url: getApp().globalData.API_Base_URL + '/formid/index/setFormId?openid=' + getApp().globalData.openid + '&formid=' +
			form_id,
	})
}
